/**
 * 轻提示
 * @param title  标题
 * @param icon  图标
 */
export const toast = (title: string = "", icon: string = "none") => {
  uni.showToast({
    icon,
    duration: 2000,
    title: title || "请求错误",
  });
};

export function guid() {
  const timestamp = new Date().getTime().toString(16);
  const randomNumber = Math.floor(Math.random() * 100000).toString(16);
  const guid = timestamp + "-" + randomNumber;
  return guid;
}
/**
 * 跳转
 * @param {string} options.url  跳转地址
 * @param {'navigateTo'|'redirectTo'|'switchTab'|'reLaunch'} options.method 跳转方式
 */
export function jump(options: any) {
  uni[options.method || "navigateTo"]({
    url: options.url,
  });
}
/**
 * 跳转
 * @param {string} options.url  跳转地址
 * @param {'navigateTo'|'redirectTo'|'switchTab'|'reLaunch'} options.method 跳转方式
 */
export function router(url: string, options: any = "") {
  uni[options || "navigateTo"]({
    url: url,
  });
}
export function calculateAge(idNumber: any) {
  if (!idNumber) return 0;
  var birthday = idNumber.substr(6, 8); // 获取出生日期部分
  var match = birthday.match(/(\d{4})(\d{2})(\d{2})/); // 提取年、月、日
  var birthYear = parseInt(match[1]);
  var birthMonth = parseInt(match[2]);
  var birthDay = parseInt(match[3]);

  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var currentMonth = currentDate.getMonth() + 1;
  var currentDay = currentDate.getDate();

  var age = currentYear - birthYear;

  if (birthYear > currentYear) {
    age = 0;
  } else if (birthYear === currentYear) {
    if (birthMonth > currentMonth) {
      age--;
    } else if (birthMonth === currentMonth && birthDay > currentDay) {
      age--;
    }
  }

  return age;
}

// 字符串增加省略符
export function shortenString(str: string, maxLength: number) {
  if (str.length <= maxLength) {
    return str;
  } else {
    return str.substring(0, maxLength) + "...";
  }
}

// 检验手机号
export function checkPhon(phone: any) {
  const regx = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (regx.test(phone)) {
    //检验成功
    return true;
  } else {
    //检验失败
    return false;
  }
}

// 检验银行卡
export function checkBankCard(number: any) {
  const regx = /^([1-9]{1})(\d{15}|\d{18})$/;
  if (regx.test(number)) {
    //检验成功
    return true;
  } else {
    //检验失败
    return false;
  }
}

export const isImg = (url = "") => {
  if (
    url.indexOf(".png") > -1 ||
    url.indexOf(".jpg") > -1 ||
    url.indexOf(".jpeg") > -1 ||
    url.indexOf(".gif") > -1 ||
    url.indexOf(".tiff") > -1 ||
    url.indexOf(".webp") > -1
  ) {
    return true;
  }
  return false;
};

export const getMediaUrl = (str: string, w?: number, h?: number): string => {
  if (isImg(str)) {
    if (w && h) {
      //?x-oss-process=image/resize,w_${w},h_${h},m_fill
      return `${str}`;
      // oss 固定宽高，缩略裁剪
      // return `${str}?x-oss-process=image/resize,m_fill,h_${h},w_${w}`;
    }
    return str;
  }
  // oss 视频第一针图片
  return str + "?x-oss-process=video/snapshot,t_0,f_jpg";
};

// 有视频显示视频第一针图片，没有显示图片
export const getImgSrc = (data: any): string => {
  if (data.videos) {
    return data.videos + "?x-oss-process=video/snapshot,t_0,f_jpg";
  }
  return data.images?.split(',')?.[0];
};
